package bg.bgfirst;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityDisplayBlackList extends Activity {
	
	private List<String> listNumero = new ArrayList<String>();
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_items);

		ApplicationBg applicationBg = (ApplicationBg) getApplication();
		TextView textView = (TextView) findViewById(R.id.textViewNbNumeros);
		
		listNumero.addAll(applicationBg.gethBlackList().values());
		String label = " "+listNumero.size()+" Blacklisted";
		textView.setText(label);
		setContentView(R.layout.activity_list_items);
		listView = (ListView) findViewById(R.id.listview);
		ArrayAdapter<String> baseAdapter= new StableArrayAdapter(this, android.R.layout.simple_list_item_1, listNumero);
		listView.setAdapter(baseAdapter);
		Button buttonClearList = (Button) findViewById(R.id.buttonClearBlackList);
		OnClickListener clicListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				getApplicationBg().gethBlackList().clear();
				showBlackList();
			}
		};
		buttonClearList.setOnClickListener(clicListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list_items, menu);
		return true;
	}
	
	public ApplicationBg getApplicationBg() {
		return (ApplicationBg) getApplication();
	}

	
	private class StableArrayAdapter extends ArrayAdapter<String> {

	    private HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

	    public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
	      super(context, textViewResourceId, objects);
	      for (int i = 0; i < objects.size(); ++i) {
	        mIdMap.put(objects.get(i), i);
	      }
	     
	    }

	    @Override
	    public long getItemId(int position) {
	      String item = getItem(position);
	      return mIdMap.get(item);
	    }

	    @Override
	    public boolean hasStableIds() {
	      return true;
	    }

	  }
	
	private void showBlackList() {
		 Intent intent = new Intent(this,ActivityDisplayBlackList.class);
		  startActivity(intent);
	}

}
